package com.tdam.g12;

import com.tdam.g12.adapters.HistorialAdapter;
import com.tdam.g12.soporte.Constantes;
import com.tdam.g12.soporte.Historial;
import com.tdma.g12.provider.HistorialProvider;
import com.tdma.g12.sql.ContactoSMSWebHelper;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.CallLog.Calls;
import android.util.Log;

public class ListaHistorial extends ListActivity
{
	private HistorialAdapter adapter;
	String[] idExterno = {};

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(com.tdam.g12.R.layout.historial_contacto);
		
		adapter = new HistorialAdapter(ListaHistorial.this);
		
		setListAdapter(adapter);
		loadHistorial();
		
	} 
	
	private void loadHistorial()
	{
		if (idExterno.length>0)
		{
			adapter.clear();
			loadHistorialContacto(idExterno);	
		}
	}
	
	private void loadHistorialContacto(String[] idExterno)
	{
		Uri uri = HistorialProvider.CONTENT_URI;
		
		Cursor cursor = getContentResolver().query(uri, null, null, idExterno, null);
		
		while (cursor.moveToNext())
		{
			String type = cursor.getString(cursor.getColumnIndex(ContactoSMSWebHelper.COL_TIPO_EVENTO));
			String date = cursor.getString(cursor.getColumnIndex(ContactoSMSWebHelper.COL_DATE_HISTORIAL));
			String idContacto = cursor.getString(cursor.getColumnIndex(ContactoSMSWebHelper.COL_ID_CONTACTO_DESTINO));
			String nombreContacto = cursor.getString(cursor.getColumnIndex(ContactoSMSWebHelper.COL_NOMBRE_CONTACTO));

			Historial itemHistorial = new Historial(type, date, idContacto, nombreContacto);
			adapter.addItem(itemHistorial);
		}

		cursor.close();
		
		adapter.notifyDataSetChanged();
	}
}
